package com.kwok.rbac.application.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kwok.rbac.application.common.utils.R;
import com.kwok.rbac.application.entity.Privilege;
import com.kwok.rbac.application.service.PrivilegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * <p>
 * 权限表 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2024-05-25
 */
@RestController
@RequestMapping("/privilege")
public class PrivilegeController {

    @Autowired
    private PrivilegeService privilegeService;

    //添加权限
    @PostMapping("/save")
    public R savePrivilege(@RequestBody Privilege privilege){
        return R.write(privilegeService.save(privilege));
    }

    //批量保存权限
    @PostMapping("/batchSave")
    public R batchSavePrivilege(@RequestBody List<Privilege> privilegeList){
        return R.write(privilegeService.saveBatch(privilegeList));
    }

    //根据主键查询单个权限
    @GetMapping("/get/{privilegeId}")
    public R getPrivilege(@PathVariable Integer privilegeId){
        return R.success(privilegeService.getById(privilegeId));
    }

    //查询所有权限
    @GetMapping("/list")
    public R getAllPrivilege(){
        return R.success(privilegeService.list());
    }

    //分页查询权限
    @PostMapping("/privilegePage")
    public Page<Privilege> getPrivilegePage(@RequestBody Page<Privilege> page){
        return privilegeService.page(page);
    }

    //根据主键修改权限信息
    @PutMapping("/update")
    public R updatePrivilege(@RequestBody Privilege privilege){
        return R.write(privilegeService.updateById(privilege));
    }

    //根据主键删除权限
    @DeleteMapping("/{privilegeId}")
    public R removePrivilege(@PathVariable Integer privilegeId){
        return R.write(privilegeService.removeById(privilegeId));
    }

    //根据主键批量删除权限
    @DeleteMapping
    public R batchRemovePrivilege(@RequestBody List<Integer> privilegeIdList){
        return R.write(privilegeService.removeByIds(privilegeIdList));
    }

    //根据角色ID给角色赋予权限(权限的批量添加)
    @PostMapping("/privilegeToRole/{roleId}")
    public R givePrivilegeToRole(@PathVariable Integer roleId, @RequestBody List<Integer> privilegeIdList){
        Boolean b = privilegeService.privilegeToRole(roleId, privilegeIdList);
        return R.write(b);
    }

    //根据角色ID获取角色所拥有的权限(权限的查询)
    @GetMapping("/getPrivilegeByRoleId/{roleId}")
    public List<Privilege> getPrivilegeByRoleId(@PathVariable Integer roleId){
        return privilegeService.getPrivilegeByRoleId(roleId);
    }

    @GetMapping("getPrivilegeByUserId/{userId}")
    public List<Privilege> getPrivilegeByUserId(@PathVariable Integer userId){
        return privilegeService.getPrivilegeByUserId(userId);
    }

}
